/**
 * Copyright (c) 2009-2011, chunquedong(YangJiandong)
 * 
 * This file is part of ChunMap project
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
 * 
 * History:
 *     2010-05-05  Jed Young  Creation
 */
package chunmap.model.geom;

import java.util.ArrayList;
import java.util.List;

import chunmap.model.coord.PrecisionModel;
import chunmap.model.elem.Envelope;

/**
 * 抽象几何体
 * 
 * @author chunquedong
 * 
 */
public abstract class AbstractGeometry implements Geometry {

	private Envelope envelop;
	private Boolean isValid;
	private PrecisionModel precision=PrecisionModel.getDoubleFloatModel();

	@Override
	public final Envelope getEnvelop() {
		if (envelop == null) {
			envelop = calculateEnvelop();
		}
		return envelop;
	}

	public void _setEnvelop(Envelope env) {
		envelop = env;
	}

	protected abstract Envelope calculateEnvelop();

	
	public GeometryCollection toGeometryCollection() {
		if (this instanceof GeometryCollection) {
			return (GeometryCollection) this;
		} else {
			List<Geometry> gs = new ArrayList<Geometry>();
			gs.add(this);
			return new MultiGeometry<Geometry>(gs);
		}
	}

	@Override
	public final boolean isValid() {
		if (isValid == null) {
			isValid = isSimple();
		}
		return isValid.booleanValue();
	}

	public void _setValid(boolean b) {
		isValid = b;
	}

	protected abstract boolean isSimple();

	@Override
	public PrecisionModel getPrecision() {
		return precision;
	}
	@Override
	public abstract boolean equals(Object obj);
	@Override
	public abstract String toString();
}